import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import pojo.FaceDectet;

import java.io.*;
import java.util.Base64;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: GaoXu
 * @Date: 2022/05/03/23:01
 * @Description:
 */
public class FaceApi {
    //设置APPID/AK/SK
    public static final String APP_ID = "";
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    public static FaceDectet faceDetcet(String url) throws IOException {
        // 初始化一个AipFace
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        File imageFile = new File(url);
        InputStream inputStream = new FileInputStream(imageFile);
        byte[] bytes = inputStream.readAllBytes();
        String res_base = Base64.getEncoder().encodeToString(bytes);


        // 调用接口
        String image = res_base;
        String imageType = "BASE64";

        // 人脸检测
        HashMap<String, Object> options = new HashMap<>();
        options.put("face_field","landmark");
        JSONObject res = client.detect(image, imageType, options);
        String result = res.toString(2);

        System.out.println(result);
        FaceDectet faceDectet = com.alibaba.fastjson.JSONObject.parseObject(result, FaceDectet.class);

        return faceDectet;
    }
}
